/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-08 10:24:03
 * @FilePath: /yt-devops/lib/components/paging/refresh.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/paging/page_controller.dart';
import 'package:yt_devops/components/paging/page_state.dart';
import 'package:yt_devops/components/tips.dart';

Widget buildRefreshWidget(
    {required Widget Function() builder,
    VoidCallback? onRefresh,
    VoidCallback? onLoad,
    required RefreshController refreshController,
    ScrollController? scrollController,
    bool enablePullUp = true,
    bool enablePullDown = true}) {
  return SmartRefresher(
    enablePullUp: enablePullUp,
    enablePullDown: enablePullDown,
    controller: refreshController,
    onRefresh: onRefresh,
    onLoading: onLoad,
    header: ListTips.classicHeader,
    footer: ListTips.generateClassicFooter,
    scrollController: scrollController,
    child: builder(),
  );
}

Widget buildRefreshListWidget<T, C extends PagingController<T, PagingState<T>>>(
    {required Widget Function(T item, int index) itemBuilder,
    bool enablePullUp = true,
    bool enablePullDown = true,
    String? tag,
    Widget Function(T item, int index)? separatorBuilder,
    Function(T item, int index)? onItemClick,
    ScrollPhysics? physics,
    bool shrinkWrap = false,
    Axis scrollDirection = Axis.vertical}) {
  C controller = Get.find(tag: tag);
  return GetBuilder<C>(
    builder: (controller) {
      return buildRefreshWidget(
        builder: () {
          if (!controller.pagingState.hasLoad) {
            return const SizedBox.shrink();
          } else if (controller.pagingState.isNoData) {
            return ListEmptyWidget(topPosition: 82);
          } else {
            return buildListView<T>(
                data: controller.pagingState.data,
                separatorBuilder: separatorBuilder,
                itemBuilder: itemBuilder,
                onItemClick: onItemClick,
                physics: physics,
                shrinkWrap: shrinkWrap,
                scrollController: controller.scrollController,
                scrollDirection: scrollDirection);
          }
        },
        refreshController: controller.refreshController,
        scrollController: controller.scrollController,
        onRefresh: controller.refreshData,
        onLoad: controller.loadMoreData,
        enablePullDown: enablePullDown,
        enablePullUp: enablePullUp && controller.pagingState.hasMore,
      );
    },
    tag: tag,
    id: controller.pagingState.refreshId,
  );
}

Widget buildListView<T>(
    {required Widget Function(T item, int index) itemBuilder,
    required List<T> data,
    Widget Function(T item, int index)? separatorBuilder,
    Function(T item, int index)? onItemClick,
    ScrollPhysics? physics,
    bool shrinkWrap = false,
    EdgeInsetsGeometry? padding,
    ScrollController? scrollController,
    Axis scrollDirection = Axis.vertical}) {
  return ListView.separated(
      shrinkWrap: shrinkWrap,
      physics: physics,
      padding: padding,
      scrollDirection: scrollDirection,
      controller: scrollController,
      itemBuilder: (ctx, index) => GestureDetector(
            child: itemBuilder.call(data[index], index),
            onTap: () => onItemClick?.call(data[index], index),
          ),
      separatorBuilder: (ctx, index) =>
          separatorBuilder?.call(data[index], index) ?? Container(),
      itemCount: data.length);
}
